home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Util / crypt / sign7800.lha / sign7800.txt < prev   
Text File  |  2004-06-20  |  5KB  |  99 lines

  1. This program will generate a valid digital signature for an Atari 7800
  2. cartridge ROM image.
  3.  
  4. The code was created by hand-decompiling the original 7800 encryption
  5. program for the Atari ST into plain C code, so it does not rely on any
  6. external libraries and should compile for almost any operating system
  7. with no muss and no fuss.
  8.  
  9.  
  10. To build it:
  11. ============
  12.  
  13.    gcc sign7800.c -o sign7800
  14.  
  15.  
  16. To run it:
  17. ==========
  18.  
  19.    sign7800 -t filename   to test for a valid existing signature only
  20.    sign7800 filename      to generate a signature printed to stdout
  21.    sign7800 -w filename   to generate a signature and update the image
  22.  
  23. 'Filename' is the name of the file with the cartridge data in it.  It
  24. must be a binary file, with a size that is a multiple of 4K bytes.  An
  25. extra 128 bytes is allowed, so that you can use it with a .A78 file.
  26.  
  27. The '-t' option only tests to see if an existing signature is valid.
  28.  
  29. The '-w' option causes the file to be updated with a new signature if
  30. it was not aready valid.  A write will not happen if there is a problem
  31. with $FFF8, $FFF9, or the reset vector, or if there is already a valid
  32. signature in the file.  Even with an already valid signature, a new
  33. signature is still computed and printed to stdout.
  34.  
  35. WARNING:  using the '-w' option should be safe if your file is really a 7800
  36. cartridge image, but if it isn't, there is a possibility that data loss
  37. to the last 128 bytes of the file could occur!  Be sure that you have a
  38. backup copy of or can re-create whatever file you use with the '-w' option!
  39.  
  40.  
  41. The rules for making a cartridge start up in Atari 7800 mode:
  42. =============================================================
  43.  
  44. The encrypted cartridge hash at $FF80-$FFF7 must be valid.  (This will not
  45. be necessary with a European 7800 console, as they do not have the crypto
  46. check.)
  47.  
  48. The high nibble of $FFF8 must be $F.
  49.  
  50. The low bit of $FFF8 must be set.  This was at some time intended for a
  51. region setting, with the USA apparently being the low bit.  So just set
  52. $FFF8 to $FF (which should mean "all regions"), and forget about it.
  53.  
  54. The high nibble of $FFF9 must be $4-$F, and should not include the bank switch
  55. memory range of a bank-switched cartridge.  This should be set to $F for
  56. fastest startup whenever possible.
  57.  
  58. The low nibble of $FFF9 must be 3 or 7.  3 will disable the k3wl Atari
  59. rainbow startup graphics, but won't make the game start any faster.  So use 7.
  60.  
  61. The reset vector at $FFFC-$FFFD must point into the hashed range.
  62.  
  63.  
  64. Technical mumbo-jumbo:
  65. ======================
  66.  
  67. The Atari 7800 encryption is based upon a hash generated from the cartridge
  68. data.  This hash is then encrypted (using the private key of P and Q which
  69. were contained in the Atari ST program) and stored in the cartridge at
  70. addresses $FF80-$FFF8.  At startup, the Atari 7800 runs its own hash of the
  71. cartridge data, then it decrypts the signature using the public key, N.  If
  72. the hash does not match the decrypted signature, the 7800 will start up the
  73. cartridge in Atari 2600 mode.
  74.  
  75. Because the signature algorithm doesn't always have a valid solution, the
  76. fourth byte in the hash is not checked for a match.  Instead, it is changed
  77. until a valid solution is found.  This usually takes four or less attempts
  78. at encryption, though I have seen it take as many as twelve.
  79.  
  80. The page range to be hashed will be a multiple of 4K, up to 48K.  This
  81. is specified in the cartridge at address $FFF9.  The high nibble of this
  82. byte refers to which page to start the hash.  The $FF00 page is handled
  83. specially, by zeroing out the bytes which will receive the signature.
  84.  
  85. One important thing to note is that the 7800 validates that the reset vector
  86. points within the hashed page range.  This was to prevent the making (either
  87. accidentally or intentionally) of a "magic key" game in which the encrypted
  88. range can be copied verbatim and otherwise ignored.  In fact, Harry Dodgson's
  89. monitor cartridge can be used this way, as only 4K is hashed, and the reset
  90. vector points to a jump to outside of the hashed range.
  91.  
  92. Before generating the signature for a cartridge, the high nibble of $FFF9
  93. must be set.  Since the hash is slow enough on a 7800 that you can notice
  94. the difference in startup time between a 4K hash and a 48K hash, it is
  95. recommended for newly written games to always set this byte to $F0 and simply
  96. make sure that the reset vector is in the $Fxxx range.
  97.  
  98. Also, when signing bank-switched games, $FFF9 should never be less than $C0
  99. to ensure that the hash will not be dependent upon the bank switch area.